﻿using SpeechLib;
using System;

namespace BoAoCoffee.Model
{
    ///https://blog.csdn.net/qq_21891843/article/details/119486747
    /// <summary>
    /// 语音播报
    /// </summary>
    public class SpeechVoice
    {
        private static SpeechVoice instance;
        private SpeechVoice() { }
        public static SpeechVoice Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SpeechVoice();
                }
                return instance;
            }
        }
        private static SpVoice m_spVoice;

        public bool VoicePlayback(string PlayText)
        {
            try
            {
                m_spVoice = new SpVoice();
                m_spVoice.Rate = -1;//语速
                m_spVoice.Volume = 100;//音量0-100
                m_spVoice.Speak(PlayText, SpeechVoiceSpeakFlags.SVSFlagsAsync);//开始// SpeechVoiceSpeakFlags.SVSFlagsAsync
                return true;
            }
            catch (Exception e)
            {
                Console.Write(e);
                return false;
            }

        }
        public bool VoicePlaybackStop()
        {
            try
            {
                m_spVoice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);//停止
                return true;
            }
            catch (Exception e)
            {
                Console.Write(e);
                return false;
            }
        }
    }
}
